<template>
  <d2-container>
	  <template slot="header">
		  <el-button type="primary" @click="change">新 增</el-button>
	  </template>
    <template>

      <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          default-expand-all
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}" >
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="departmentName"
            label="部门名称"
            >
          </el-table-column>
          <el-table-column
            prop="departmentCode"
            label="部门编码" >
          </el-table-column>
          <el-table-column
            prop="personInCharge"
            label="领导人">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="联系电话">
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="180">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="updateRow(scope.row.id)">编辑</el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row)"
                size="small">
                移除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

    </template>
    <template>
      <div>
          <keep-alive>
              <addPage :addPage="addComponent" @on-addPage-change="onAddPageChange" @on-loadlist-change="fetchData" ></addPage>
            </keep-alive>
      </div>
      <div>
          <keep-alive>
              <updatePage @on-loadlist-change="fetchData"  :uShow="updateComponent" @on-uShow-change="onUShowChange" :uId="updateId" @on-id-change="onIdChange"></updatePage>
            </keep-alive>
      </div>
    </template>
  </d2-container>
</template>

<script>
import { DepartmentList, DelDepartment } from '@api/department/department'
import addPage from './add'
import updatePage from './update'
import dayjs from 'dayjs'

export default {
  name: 'system-route',
  components: {
    addPage, updatePage
  },
  data () {
    return {
      addComponent: false,
      updateComponent: false,
      updateId: '',
      tableData: [],
      multipleSelection: []
    }
  },

  mounted () {
    this.fetchData()
  },
  methods: {
    /** 双向绑定 start */
    change () {
      this.addComponent = !this.addComponent
    },
    onAddPageChange (val) {
      this.addComponent = val// ④外层调用组件方注册变更方法，将组件内的数据变更，同步到组件外的数据状态中
    },
    uChange () {
      this.updateComponent = !this.updateComponent
    },
    onUShowChange (val) {
      this.updateComponent = val// ④外层调用组件方注册变更方法，将组件内的数据变更，同步到组件外的数据状态中
    },
    idChange (val) {
      this.updateId = val
    },
    onIdChange (val) {
      this.updateId = val// ④外层调用组件方注册变更方法，将组件内的数据变更，同步到组件外的数据状态中
    },
    /** 双向绑定 end */

    fetchData () {
      this.loading = true
      DepartmentList({ parentId: -1 }).then(res => {
        this.tableData = res.data
        this.loading = false
      }).catch(err => {
        console.log('err', err)
        this.loading = false
      })
    },
    deleteRow (row) {
      //  console.log( JSON.stringify(row))
      // rows.splice(index, 1)

      this.$confirm('确定删除吗').then(() => {
        let id = row.id
        this.loading = true
        DelDepartment({ departmentId: id }).then(res => {
          this.fetchData()
          this.loading = false
        }).catch(err => {
          console.log('err', err)
          this.loading = false
        }) // function(done)，done 用于关闭 Dialog
        done()
      }).catch(() => {
        // console.log("取消操作");
      })
    },
    updateRow (id) {
      this.uChange()
      this.idChange(id)
    },
    forDate (row, column) {
      return row.createTime == null ? '' : dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>
